package com.njwb.subject.service.impl;

import com.njwb.subject.json.SubjectJson;
import com.njwb.subject.mapper.SubjectMapper;
import com.njwb.subject.pojo.Subject;
import com.njwb.subject.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.channels.InterruptedByTimeoutException;
import java.util.List;
import java.util.Set;

@Service

public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void load() {
        //加载
        int minNo = subjectMapper.getMinNo();
        int maxNo = subjectMapper.getMaxNo();
        List<Integer> nos = subjectMapper.getUnselectedNos();
        //操作redis
        //存储编号最小值和最大值
        String minKey = "min_no";
        String maxKey = "max_no";
        redisTemplate.opsForValue().set(minKey,minNo);
        redisTemplate.opsForValue().set(maxKey,maxNo);
        //存储未被选中的编号
        nos.forEach(no->{
            String noKey = "no_" + no;
            redisTemplate.opsForValue().set(noKey,no);
        });

    }
    @Transactional
    @Override
    public SubjectJson extract(String sno, String sname) {
        SubjectJson subjectJson = null;

        //1.根据学号查询课题信息
        Subject subject = subjectMapper.getBySNo(sno);

        if(subject != null){//已经选过了
            //提示。。。。。。。。。
            subjectJson = new SubjectJson();
            subjectJson.setSubjectName(subject.getSubjectName());
            subjectJson.setTeacherName(subject.getTeacherName());
            subjectJson.setTeacherContact(subject.getTeacherContact());
            subjectJson.setStatus("1");

        }else{//没有选过
            //2.判断是否有足够的课题供选择
            Set keySet = redisTemplate.keys("no_*");
            if(keySet.size() == 0){//没有足够的课题
                //提示。。。。。。。。。
                return null;
            }else{//有足够的课题
                //随机生成课程编号
                int minNo = (Integer)( redisTemplate.opsForValue().get("min_no"));
                int maxNo = (Integer)( redisTemplate.opsForValue().get("max_no"));
                while(true){
                    int randomNo = (int)(Math.random()*(maxNo - minNo + 1) + minNo);
                    String key = "no_" + randomNo;
                    if(redisTemplate.hasKey(key)){//含有
                        //抽中的课题编号
                        int value = (Integer) redisTemplate.opsForValue().get(key);
                        Subject selectedSubject = subjectMapper.getByNo(randomNo);
                        selectedSubject.setSno(sno);
                        selectedSubject.setSname(sname);
                        //更新数据库
                        subjectMapper.update(selectedSubject);
                        //将redis中的相应课题编号剔除
                        redisTemplate.delete(key);

                        subjectJson = new SubjectJson();
                        subjectJson.setSubjectName(selectedSubject.getSubjectName());
                        subjectJson.setTeacherName(selectedSubject.getTeacherName());
                        subjectJson.setTeacherContact(selectedSubject.getTeacherContact());
                        subjectJson.setStatus("2");

                        break;
                    }
                }


            }

        }

        return subjectJson;
    }
}
